<template>
  <div>
    <!-- 头部导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/goodsList' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>扩展</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图修改</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form
      :model="carouselform"
      :rules="rules"
      ref="carouselform"
      label-width="100px"
      class="demo-carouselform"
      style="margin-top:20px"
    >
      <el-form-item label="超链接到" prop="linkto">
        <el-input v-model="carouselform.linkto"></el-input>
      </el-form-item>
      <el-form-item label="显示顺序" prop="name">
        <el-input-number v-model="carouselform.showorder" :min="1" label="描述文字"></el-input-number>
      </el-form-item>

      <el-form-item label="是否显示" prop="isshow">
        <el-switch v-model="carouselform.isshow"></el-switch>
      </el-form-item>
      <el-form-item label="上传图片" prop="isshow">
        <el-upload
          class="avatar-uploader"
          action="/files"
          :show-file-list="false"
          :on-success="handleSuccess"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('carouselform')">修改</el-button>
        <el-button type="warning" @click="$router.push('/carouselList')">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
    props:["_id"],
  data() {
    return {
      carouselform: {
        // linkto: "",
        // localsrc: "",
        // isshow: true,
        // showorder: 1
      },
      rules: {
        linkto: [
          {
            required: true,
            message: "请输入链接地址例如www.baidu.com",
            trigger: "blur"
          }
        ]
      },
      imageUrl: ""
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios.put("/carousel", this.carouselform).then(res => {
            if (res.data) {
              this.$message({
                message: "修改成功",
                type: "success"
              });
            } else {
              this.$message.error("修改失败");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSuccess(res, file, fileList) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.carouselform.localsrc = res;

      //   console.log(file);
      //   console.log(fileList);
    },
    getOne(){
        axios
            .get(`/carousel/${this._id}`)
            .then(res=>{
                if(res.data){
                    this.carouselform = res.data[0];
                    this.imageUrl = res.data[0].localsrc;
                }
            })
    }
  },
  mounted(){
      this.getOne();
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 360px;
  height: 180px;
  line-height: 180px;
  text-align: center;
}
.avatar {
  width: 360px;
  height: 180px;
  display: block;
}
.el-form {
  width: 460px;
}
</style>
